# -*- coding: utf-8 -*-
from PyQt5.QtCore import QThread, pyqtSignal
from socket import socket


class ClientThread(QThread):
    message = pyqtSignal(str)
    error = pyqtSignal()
    connect_error = pyqtSignal()

    def __init__(self, client_socket: socket):
        super().__init__()
        self.client_socket = client_socket

    def run(self):
        try:
            self.client_socket.connect(("172.20.23.118", 8080))
            while True:
                try:
                    receive_message = self.client_socket.recv(512)
                    self.message.emit(receive_message.decode('utf-8'))
                except Exception as e:
                    break
        except TimeoutError:
            self.connect_error.emit()
